package com.all.play.myadaptertoadapter.secondadapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.all.play.myadaptertoadapter.R;
import com.all.play.myadaptertoadapter.bean.MyBean;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/5/18.
 * Time: 2018/5/18  8:51
 */

public class MySecondAdapter extends RecyclerView.Adapter {

    public static final int ITEM_TYPE_ONE=0;
    public static final int ITEM_TYPE_TWO=1;
    private int CURRENT_TYPE=ITEM_TYPE_ONE;


    private Context context;
    private MyBean.ResultBean result;
    private LayoutInflater inflater;

    public MySecondAdapter(Context context, MyBean.ResultBean result) {

        this.context = context;
        this.result = result;

        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if(viewType==ITEM_TYPE_ONE){
            View view = inflater.inflate(R.layout.item_one, parent, false);
            return new MyOneViewHolder(view);

        }else if (viewType==ITEM_TYPE_TWO){
            View view = inflater.inflate(R.layout.item_two, parent, false);
            return new MyTwoViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if(holder instanceof MyOneViewHolder){
            List<MyBean.ResultBean.ActInfoBean> act_info = result.getAct_info();
            if(act_info!=null&&act_info.size()>0){
                MyOneViewHolder myOneViewHolder= (MyOneViewHolder) holder;
                myOneViewHolder.setData(context,act_info);
            }

        }else if(holder instanceof MyTwoViewHolder){
            List<MyBean.ResultBean.BannerInfoBean> banner_info = result.getBanner_info();
            if (banner_info!=null&&banner_info.size()>0){
                MyTwoViewHolder myTwoViewHolder= (MyTwoViewHolder) holder;
                myTwoViewHolder.setData(context,banner_info);
            }
        }



    }

    @Override
    public int getItemCount() {

        return result==null?0:result.getSize();
    }


    @Override
    public int getItemViewType(int position) {
        switch(position){
            case ITEM_TYPE_ONE:
                CURRENT_TYPE=ITEM_TYPE_ONE;
                break;
            case ITEM_TYPE_TWO:
                CURRENT_TYPE=ITEM_TYPE_TWO;
                break;
            default:

                break;
        }

        return CURRENT_TYPE;
    }

    class MyOneViewHolder extends RecyclerView.ViewHolder{
        RecyclerView rv_item_one;
        public MyOneViewHolder(View itemView) {
            super(itemView);
            rv_item_one=itemView.findViewById(R.id.rv_item_one);


        }

        public void setData(Context context, List<MyBean.ResultBean.ActInfoBean> act_info) {
            MySecondItemOneAdapter oneAdapter = new MySecondItemOneAdapter(context,act_info);
            rv_item_one.setAdapter(oneAdapter);

            rv_item_one.setLayoutManager(new GridLayoutManager(context,4));

        }
    }

    class MyTwoViewHolder extends RecyclerView.ViewHolder{
        RecyclerView rv_second;
        public MyTwoViewHolder(View itemView) {
            super(itemView);
            rv_second=itemView.findViewById(R.id.rv_second);



        }

        public void setData(Context context, List<MyBean.ResultBean.BannerInfoBean> banner_info) {
            MySecondItemTwoAdapter twoAdapter = new MySecondItemTwoAdapter(context, banner_info);
            rv_second.setAdapter(twoAdapter);

            rv_second.setLayoutManager(new GridLayoutManager(context,3));



        }
    }
}
